package tr3dtris.game.controller;

import java.awt.Component;
import tr3dtris.game.Game;
import tr3dtris.game.GameActions;
import tr3dtris.game.controller.inputMapper.AbstractInputMapper;
import tr3dtris.game.controller.inputMapper.DefaultInputMapper;

/**
 * <Controller> class. Used to connect a game instance with an input management
 * system. That is practically a connection between a <Game> instance and a
 * <ControllerListener> instance.
 * @author Luciano Mammino <loige@hotmail.com>
 * @author Andrea Mangano <kapitan0@hotmail.it>
 * @version 0.1
 * @since 0.1
 */
public class Controller
{

    /**
     * Reference to the <ControllerListener>
     */
    private Game game;
    private GameActions gameActions;
    private ControllerListener listener;

    /**
     * Constructor. Creates a new Controller from a given <Game> instance. It
     * uses a new <DefaultInputMapper> instance to map the user input.
     * @param game
     */
    public Controller(Game game)
    {
        this(game, new DefaultInputMapper(game.getGameActions()));
    }

    /**
     * Constructor. Creates a new Controller using a given <Game> instance and
     * a given <AbstractInputMapper> instance
     * @param game
     * @param inputMapper
     */
    public Controller(Game game, AbstractInputMapper inputMapper)
    {
        this(game, inputMapper, null);
    }

    /**
     * Constructor. Creates a new Controller using a given <Game> instance and
     * a given <AbstractInputMapper> instance and attach the listener to a given
     * <Component>.
     * @param game
     * @param inputMapper
     * @param listenerAttachComponent
     */
    public Controller (Game game, AbstractInputMapper inputMapper, Component listenerAttachComponent)
    {
        this.game = game;
        this.gameActions = this.game.getGameActions();
        this.listener = new ControllerListener(this.getGameActions(), inputMapper);
        if(listenerAttachComponent != null)
            this.attachListener(listenerAttachComponent);
    }

    /**
     * Attach the listener to a given <Component>.
     * @param listenerAttachComponent
     */
    public void attachListener(Component listenerAttachComponent)
    {
        listenerAttachComponent.addKeyListener(this.getListener());
    }

    /**
     * @return the game
     */
    public Game getGame() {
        return game;
    }

    /**
     * @return the gameActions
     */
    public GameActions getGameActions() {
        return gameActions;
    }

    /**
     * @return the listener
     */
    public ControllerListener getListener() {
        return listener;
    }

}